﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Jogo jogo;
        private PictureBox[] pbx;
        private Image cloneIni, cloneSombra;
        private Jogador jogador;
        private SoundPlayer sdJogShot, sdIniShot;

        public Form1()
        {
            jogo = new Jogo();
            jogador = new Jogador();

            InitializeComponent();
            pbx = new PictureBox[5];           
            
            
            pbx[0] = pbInimigo1; pbInimigo1.Visible = false;
            pbx[1] = pbInimigo2; pbInimigo2.Visible = false;
            pbx[2] = pbInimigo3; pbInimigo3.Visible = false;
            pbx[3] = pbInimigo4; pbInimigo4.Visible = false;
            pbx[4] = pbInimigo5; pbInimigo5.Visible = false;
            pbMascara.Location = pbGame.Location;
            tsMensagem.Text = "";

            
            // Graphics permite pintar a imagem de Mascara do jogo
         //   graf = pbMascara.CreateGraphics();
//            pbMascara.f
            //            cloneIni = pbInimigo1.Image;
            timer1.Interval = 3000;
            //Récupération du bitmap

        }



        private void iniciarToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (jogo.Started)
            {
                jogo.restart();

                pbInimigo1.Visible = false;
                pbInimigo2.Visible = false;
                pbInimigo3.Visible = false;
                pbInimigo4.Visible = false;
                pbInimigo5.Visible = false;
                tsMensagem.Text = "";
            }
            jogador = new Jogador();
            atualizaStatusBar(true);
            timer2.Stop();
            timer1.Start();  // Inicia o Timer1 para aparecimento do Inimigo
        }

        private void drawInimigos(object sender, EventArgs e)
        {
            Inimigo ini;
            ini = jogo.getInimigos();
            if (ini != null)
            {
                int i = ini.Id;
                pbx[i].Width = Inimigo.WIDTH;
                pbx[i].Height = Inimigo.HEIGHT;
                pbx[i].SizeMode = PictureBoxSizeMode.StretchImage;
                pbx[i].Scale(ini.Height, ini.Height);
                pbx[i].Location = new Point((ini.X + pbGame.Location.X), (ini.Y + pbGame.Location.Y));
                pbx[i].BringToFront();
                pbx[i].Visible = ini.getStatus();
                
                if(!jogo.getAtirar())
                     timer2.Start();   // inicia o timer2 para o Tiro do inimigo

                jogo.inimigosAtirar();
            }
            timer1.Start();
        }


        private void apagarInimigo(int id)
        {
            pbx[id].Visible = false;
            if(!jogo.getAtirar())
                  paraTiroInimigo();
            //timer1.start();
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            drawInimigos(sender, e);


        }

        private void pbGame_Click(object sender, EventArgs e)
        {

        }

        private void validaTiro(object sender, MouseEventArgs e, int mx, int my)
        {
            /**
                 * aqui teriamos o caso do tiro ocorrer
            * */

            int x, y,id ;
            id = -1;
            if (e.Button.ToString().Equals("Left") && jogador.TemBalas)
            {
                if (!pbGame.Equals(sender))
                {
                    for (int i = 0; i < Jogo.MAX_INIMIGO; i++)
                        if (pbx[i].Equals(sender))
                            id = i;

                    //  pbMascara.BringToFront();
                    //  Rectangle destRect1 = pbx[id].Bounds;
                    //  Graphics graf = Graphics.FromImage(pbMascara.Image);
                    //  graf.Graphics.DrawImage(pbSombra, destRect1, x, y, pbx[id].Width, pbx[id].Height, units);

                    //GraphicsUnit units  = GraphicsUnit.Pixel;               
                    Bitmap bmp = new Bitmap(pbSombra.Image, pbx[id].Size);

                    // MessageBox.Show("x:" + e.X + "y:" + e.Y + " i:" + id + " h: " + pbx[id].Height + "w " + pbx[id].Width);

                    //MessageBox.Show("Entrou AAAA" + bmp.GetPixel(e.X, e.Y).R + "" + bmp.GetPixel(e.X, e.Y).G + "" + bmp.GetPixel(e.X, e.Y).B);
                    if (bmp.GetPixel(e.X, e.Y).Equals(Color.FromArgb(42, 42, 42)))
                    {
                        //MessageBox.Show(" //:" + ((float )e.Y / pbx[id].Height) + " e.y:" + e.Y +" Height: "+ pbx[id].Height);
                        float dano = ((float)e.Y / pbx[id].Height < 0.25f ? 1f : 0.5f);
                        int pontos = (int)(dano * Jogo.HEIGHT);
                        jogador.atiraNoInimigo(pontos);
                        if (jogo.morreInimigo(id, dano))
                            apagarInimigo(id);

                    }
                    else 
                        jogador.atiraNoInimigo(0);
                }

                /**
                 *  Trecho dedicado a tratar o som do tiro do Jogador
                 * */
                if (sdJogShot != null)
                {
                    if (sdJogShot.IsLoadCompleted)
                    {
                        if (sdIniShot != null && sdIniShot.IsLoadCompleted)
                        {
                           // sdIniShot.Stop();
                            
                            
                        }
                        sdJogShot.Play();
                    }

                }
                else 
                {
                    sdJogShot = new SoundPlayer(WindowsFormsApplication1.Properties.Resources.pistolshot);
                    sdJogShot.Play();
                }
                
            }
            else
                if (e.Button.ToString().Equals( "Right"))
                {
                    jogador.recarregarPent();

                }
            // Cursor.Current = Cursors.WaitCursor;
            atualizaStatusBar(e.Button.ToString().Equals("Left"));
        }

        private void atualizaStatusBar(bool dirmouse)
        {

            stbarBalas.Text = "Balas: "+jogador.Balas.ToString();
            stbarPents.Text = "Pents: " + jogador.Pents.ToString();
            stbarScore.Text = "Score: " + jogador.Score.ToString();
            if (dirmouse)
            {
                if (!jogador.TemBalas)
                {
                    tsMensagem.Text = " Você tá sem MUNIÇÃO, RECARREGUE!!!";
                    this.Cursor = Cursors.No;
                }
                else
                {
                    this.Cursor = Cursors.WaitCursor;
                    tsMensagem.Text = "";
                }
            }

        }

        private void pbInimigo3_Click(object sender, EventArgs e)
        {
        }

        private void pbInimigo4_Click(object sender, EventArgs e)
        {
        }

        private void pbInimigo5_Click(object sender, EventArgs e)
        {
        }

        private void pbInimigo2_Click(object sender, EventArgs e)
        {



        }

        private void pbInimigo1_Click(object sender, EventArgs e)
        {
        }

        private void pbGame_MouseDown(object sender, MouseEventArgs e)
        {
            validaTiro(sender, e, Form1.MousePosition.X, Form1.MousePosition.Y);
        }

        private void pbInimigo4_MouseDown(object sender, MouseEventArgs e)
        {
            validaTiro(sender, e, Form1.MousePosition.X, Form1.MousePosition.Y);

        }

        private void pbInimigo5_MouseDown(object sender, MouseEventArgs e)
        {
            validaTiro(sender, e, Form1.MousePosition.X, Form1.MousePosition.Y);
        }

        private void pbInimigo3_MouseDown(object sender, MouseEventArgs e)
        {
            validaTiro(sender, e, Form1.MousePosition.X, Form1.MousePosition.Y);
        }

        private void pbInimigo2_MouseDown(object sender, MouseEventArgs e)
        {
            validaTiro(sender, e, Form1.MousePosition.X, Form1.MousePosition.Y);
        }

        private void pbInimigo1_MouseDown(object sender, MouseEventArgs e)
        {
            validaTiro(sender, e, Form1.MousePosition.X, Form1.MousePosition.Y);
        }

        private void sairToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void toolStripStatusLabel2_Click(object sender, EventArgs e)
        {

        }

        private void toolStripStatusLabel3_Click(object sender, EventArgs e)
        {

        }

        private void opcToolStripMenuItem_Click(object sender, EventArgs e)
        {
                
        }

        private void usarMouseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (usarMouveMenu.Checked)
            {
             //   miAutoRecarga.Enabled = true;
                miUsarVoz.Checked = false;



            }
            
            
         }

        private void miAutoRecarga_Click(object sender, EventArgs e)
        {
            jogador.Autocarga = miAutoRecarga.Checked;
        }

        private void miUsarVoz_Click(object sender, EventArgs e)
        {
            if (miUsarVoz.Checked)
            {
           //     miAutoRecarga.Enabled = true;
                usarMouveMenu.Checked = false;
            }
        }

        private void pbMascara_Click(object sender, EventArgs e)
        {

        }

        private void pbMascara_MouseDown(object sender, MouseEventArgs e)
        {
            jogador.atiraNoInimigo(0);
        }

        private void menuStrip1_Paint(object sender, PaintEventArgs e)
        {



            //for (int i = 0; i < Jogo.MAX_INIMIGO; i++)

            //{
            //    if (pbx[i].Visible)
            //    {
            //        // Create a Bitmap object from an image file.
            //        Bitmap myBitmap = new Bitmap(pbx[i].Image);
            //        // Draw myBitmap to the screen.
            //        e.Graphics.DrawImage(
            //        myBitmap,
            //        pbx[i].Location.X,
            //        pbx[i].Location.Y,
            //        myBitmap.Width,
            //        myBitmap.Height);
            //        // Get the color of a background pixel.
            //        Color backColor = myBitmap.GetPixel(1, 1);
            //        // Make backColor transparent for myBitmap.
            //        myBitmap.MakeTransparent(backColor);
            //        // Draw the transparent bitmap to the screen.
            //        e.Graphics.DrawImage(
            //        myBitmap,
            //        myBitmap.Width,
            //        0,
            //        myBitmap.Width,
            //        myBitmap.Height);
            //    }
            //}

        }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {
            
        }

        private void paraTiroInimigo()
        {
            sdIniShot.Stop();
        }

        /**
         * 
         *  Funcao que usa a ativacao do Timer2 para iniciar o Som do tiro Inimigo
         * */
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (jogo.getAtirar())   // verifica se existem inimigos atirando de fato.
            {
                if (sdIniShot != null)
                {
                    if (sdIniShot.IsLoadCompleted)
                    {

                        sdIniShot.PlayLooping();
                    }
                }
                else
                {
                    sdIniShot = new SoundPlayer(WindowsFormsApplication1.Properties.Resources.singlegunshot);
                    sdIniShot.PlayLooping();

                }
            } 
        }
    }
}
